/**
 * @Description: 数泽MES
 * @Author: 数泽
 * @Date: 2023-10-27 16:08:02
 **/
import {nextTick} from "vue"
import {useAppStore} from "@/store/modules/app.js";
import {crmFieldColumnWidthAPI, crmPoolFieldColumnWidthAPI} from "@/api/crm/common.js";

export function useSzTable() {


  /**
   * 列表操作
   * @param crmOption
   * @param elTableOption
   * @param row
   * @param column
   */
  function bizHandleRowClick(crmOption, elTableOption, fieldAndListOption, row, column) {
    if (column.type === 'selection') {
      return // 多选布局不能点击
    }
    if (crmOption.crmType === 'leads') {
      if (column.property === 'leadsName') {
        elTableOption.rowID = row.leadsId
        elTableOption.rowType = 'leads'
        crmOption.showDview = true
      } else {
        crmOption.showDview = false
      }
    } else if (crmOption.crmType === 'customer') {
      if (column.property === 'businessCheck' && row.businessCount > 0) {
        return // 列表查看商机不展示详情
      }
      if (column.property === 'customerName') {
        elTableOption.rowID = row.customerId
        elTableOption.rowType = 'customer'
        crmOption.showDview = true
      } else {
        crmOption.showDview = false
      }
    } else if (crmOption.crmType === 'contacts') {
      if (column.property === 'customerName') {
        elTableOption.rowID = row.customerId
        elTableOption.rowType = 'customer'
        crmOption.showDview = true
      } else if (column.property === 'name') {
        elTableOption.rowID = row.contactsId
        elTableOption.rowType = 'contacts'
        crmOption.showDview = true
      } else {
        crmOption.showDview = false
      }
    } else if (crmOption.crmType === 'business') {
      if (column.property === 'customerName') {
        elTableOption.rowID = row.customerId
        elTableOption.rowType = 'customer'
        crmOption.showDview = true
      } else if (column.property === 'businessName') {
        elTableOption.rowID = row.businessId
        elTableOption.rowType = 'business'
        crmOption.showDview = true
      } else {
        crmOption.showDview = false
      }
    } else if (crmOption.crmType === 'contract') {
      if (column.property === 'customerName') {
        elTableOption.rowID = row.customerId
        elTableOption.rowType = 'customer'
        crmOption.showDview = true
      } else if (column.property === 'businessName') {
        elTableOption.rowID = row.businessId
        elTableOption.rowType = 'business'
        crmOption.showDview = true
      } else if (column.property === 'contactsName') {
        elTableOption.rowID = row.contactsId
        elTableOption.rowType = 'contacts'
        crmOption.showDview = true
      } else if (column.property === 'num') {
        elTableOption.rowID = row.contractId
        elTableOption.rowType = 'contract'
        crmOption.showDview = true
      } else {
        crmOption.showDview = false
      }
    } else if (crmOption.crmType === 'product') {
      if (column.property === 'name') {
        elTableOption.rowID = row.productId
        elTableOption.rowType = 'product'
        crmOption.showDview = true
      } else {
        crmOption.showDview = false
      }
    } else if (crmOption.crmType === 'receivables') {
      if (column.property === 'customerName') {
        elTableOption.rowID = row.customerId
        elTableOption.rowType = 'customer'
        crmOption.showDview = true
      } else if (column.property === 'contractNum') {
        elTableOption.rowID = row.contractId
        elTableOption.rowType = 'contract'
        crmOption.showDview = true
      } else if (column.property === 'number') {
        elTableOption.rowID = row.receivablesId
        elTableOption.rowType = 'receivables'
        crmOption.showDview = true
      } else {
        crmOption.showDview = false
      }
    } else if (crmOption.crmType == 'visit') {
      if (column.property === 'visitNumber') {
        elTableOption.rowID = row.visitId
        elTableOption.rowType = 'visit'
        crmOption.showDview = true
      } else if (column.property === 'customerName') {
        elTableOption.rowID = row.customerId
        elTableOption.rowType = 'customer'
        crmOption.showDview = true
      } else if (column.property === 'contractNum') {
        elTableOption.rowID = row.contractId
        elTableOption.rowType = 'contract'
        crmOption.showDview = true
      } else if (column.property === 'contactsName') {
        elTableOption.rowID = row.contactsId
        elTableOption.rowType = 'contacts'
        crmOption.showDview = true
      } else {
        crmOption.showDview = false
      }
    } else if (crmOption.crmType == 'invoice') {
      if (column.property === 'customerName') {
        elTableOption.rowID = row.customerId
        elTableOption.rowType = 'customer'
        crmOption.showDview = true
      } else if (column.property === 'contractNum') {
        elTableOption.rowID = row.contractId
        elTableOption.rowType = 'contract'
        crmOption.showDview = true
      } else if (column.property === 'invoiceApplyNumber') {
        elTableOption.rowID = row.invoiceId
        elTableOption.rowType = 'invoice'
        crmOption.showDview = true
      } else {
        crmOption.showDview = false
      }
    } else if (crmOption.crmType == 'receivablesPlan') {
      console.log("receivablesPlan",row)
      if (column.property === 'customerName') {
        elTableOption.rowID = row.customerId
        elTableOption.rowType = 'customer'
        crmOption.showDview = true
      } else if (column.property === 'contractNum') {
        elTableOption.rowID = row.contractId
        elTableOption.rowType = 'contract'
        crmOption.showDview = true
      } else if (column.property === 'num') {
        elTableOption.rowID = row.receivablesPlanId
        elTableOption.rowType = 'receivablesPlan'
        crmOption.showDview = true
      } else {
        crmOption.showDview = false
      }
    }
    elTableOption.rowIndex = getRowIndex(elTableOption, fieldAndListOption)

    if (crmOption.showDview) {
      const appStore = useAppStore();
      appStore.SET_COLLAPSE(crmOption.showDview)
    }
    return {crmOption, elTableOption}
  }

  /**
   * 字段排序
   */
  function sortChange(crmOption, pageOption, column, prop, order) {
    pageOption.currentPage = 1
    crmOption.sortData = column || {}
  }

  // 当拖动表头改变了列的宽度的时候会触发该事件
  function bizHandleHeaderDragend(crmOption, fieldAndListOption, newWidth, oldWidth, column) {
    console.log("handleHeaderDragend", newWidth, oldWidth, column)
    if (column.property) {
      let request = null
      let sortId
      fieldAndListOption.fieldList.forEach(e => {
        if (column.property === e.prop) {
          sortId = e.sortId
        }
      })
      if (!sortId) {
        return
      }
      const params = {
        id: sortId,
        width: newWidth,
        field: column.property
      }
      if (crmOption.isSeas) {
        // 如果是公海的情况
        if (!crmOption.poolId) {
          return
        }
        request = crmPoolFieldColumnWidthAPI
        params.poolId = crmOption.poolId
      } else {
        request = crmFieldColumnWidthAPI
      }

      request(params).then(res => {
      }).catch(() => {
      })
    }
  }

  function bizHandleSelectionChange(elTableOption, config, mainTable, val) {
    if (elTableOption.ignoreSelectedChange) {
      return
    }
    // 单选操作
    if (config.value.radio && val.length > 1) {
      const lastObj = val[val.length - 1]
      elTableOption.ignoreSelectedChange = true
      mainTable.clearSelection()
      nextTick(() => {
        elTableOption.ignoreSelectedChange = false
        mainTable.toggleRowSelection(lastObj)
      })
      return
    } else {
      elTableOption.selectionList = val
    }
  }

  /**
   * 获取点击行索引
   */
  function getRowIndex(elTableOption, fieldAndListOption) {
    let rowIndex = 0
    for (let index = 0; index < fieldAndListOption.dataList.length; index++) {
      const element = fieldAndListOption.dataList[index]
      if (element[`${elTableOption.rowType}Id`] === elTableOption.rowID) {
        rowIndex = index
        break
      }
    }
    return rowIndex
  }

  return {
    bizHandleRowClick, sortChange, bizHandleHeaderDragend, bizHandleSelectionChange,
  }
}